package com.atguigu.suanfa;

/**
 * @Author: lijunyao
 * @Date: 2025/02/07 12:04
 * 二分查找法
 */
public class T1 {
    public static int binarySearch(int[] arraySorted,int target){
        int left = 0;
        int right = arraySorted.length - 1;
        while (left <= right){
            int middle = left + (right - left)/2;
            if(arraySorted[middle] == target){
                return middle;
            } else if (arraySorted[middle] > target){
                right = middle - 1;
            }else {
                left = middle + 1;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] arrays = new int[]{1,11,22,33,77,99,103};
        int target = 11;
        System.out.println(binarySearch(arrays,target));
    }
}